/**
 *author:TruongNH.(nguyenhuutruong.20111991@gmail.com)
 *created date: Dec 21, 2014
 *version: 1.0
 */
package vn.mta.nht.mock.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import vn.mta.nht.mock.business.PremiseBO;
import vn.mta.nht.mock.common.vo.PremiseVO;
import vn.mta.nht.mock.entity.Premise;

@Controller
public class PremiseController {
	static final Logger logger = Logger.getLogger(PremiseController.class);

	@Autowired
	private PremiseBO premiseBO;

	@RequestMapping("/register")
	public ModelAndView getRegisterForm(
			@ModelAttribute("premiseVO") PremiseVO premiseVO,
			BindingResult result) {

		ArrayList<String> accreditations = new ArrayList<String>();
		accreditations.add("ISO 9001");
		accreditations.add("ISO 14001");
		accreditations.add("ISO 27001");

		ArrayList<String> jspOffices = new ArrayList<String>();
		jspOffices.add("A");
		jspOffices.add("B");
		jspOffices.add("C");

		ArrayList<String> localDemographicIssues = new ArrayList<String>();
		localDemographicIssues.add("High");
		localDemographicIssues.add("Rual");

		ArrayList<String> outreachLocation = new ArrayList<String>();
		outreachLocation.add("Test 1");
		outreachLocation.add("Test 2");

		ArrayList<String> localHotel = new ArrayList<String>();
		localHotel.add("Hotel California");
		localHotel.add("Hotel Vinh Yen");

		Map<String, String> cateringType = new HashMap<String, String>();
		cateringType.put("ct 1", "ct 1");
		cateringType.put("ct 2", "ct 2");

		Map<String, String> iS_Network = new HashMap<String, String>();
		iS_Network.put("Open", "Open");
		iS_Network.put("Wid", "Wid");
		iS_Network.put("Closed", "Closed");

		Map<String, Object> model = new HashMap<String, Object>();
		model.put("accreditations", accreditations);
		model.put("jspOffices", jspOffices);
		model.put("localDemographicIssues", localDemographicIssues);
		model.put("outreachLocation", outreachLocation);
		model.put("localHotel", localHotel);
		model.put("cateringType", cateringType);
		model.put("iS_Network", iS_Network);

		return new ModelAndView("Premise", "model", model);
	}

	@RequestMapping("/savePremise")
	/**
	 * This method uses to add a Premise
	 */
	public ModelAndView savePremiseData(
			@ModelAttribute("premiseVO") PremiseVO premiseVO,
			BindingResult result) {
		Map<String, Object> model = new HashMap<String, Object>();

		try {
			premiseBO.save(premiseVO.converter(premiseVO));
			List<Premise> list = premiseBO.findAll();
			PremiseVO preVO = new PremiseVO();
			model.put("premiseList", preVO.converterListEntity2VO(list));

		} catch (Exception e) {
			logger.error("HomeController:: savePremiseData:: Exception:: "
					+ e.getMessage());
		}

		return new ModelAndView("ListPremise", "model", model);

	}

	@RequestMapping("/premiseList")
	public ModelAndView getPremiseList() {

		Map<String, Object> model = new HashMap<String, Object>();
		try {
			List<Premise> list = premiseBO.findAll();
			PremiseVO preVO = new PremiseVO();
			model.put("premiseList", preVO.converterListEntity2VO(list));
		} catch (Exception e) {
			logger.error("HomeController:: getPremiseList:: Exception:: "
					+ e.getMessage());
			System.out.println("HomeController:: getPremiseList:: Exception:: "
					+ e.getMessage());
		}
		return new ModelAndView("ListPremise", "model", model);
	}
}
